<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          @click="handleSearchList()"
          type="primary"
          size="small">
          查询
        </el-button>
        <el-button
          style="float: right;margin-right: 15px"
          @click="handleResetSearch()"
          size="small">
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="输入搜索：">
            <el-input style="width: 203px" v-model="listQuery.keyword" placeholder="输入标题"></el-input>
          </el-form-item>
          <el-form-item label="图片归属：">
            <el-select v-model="listQuery.state" placeholder="全部">
              <el-option
                v-for="item in swiperStats"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="链接类型：">
            <el-select v-model="listQuery.type" placeholder="全部">
              <el-option
                v-for="item in swiperTypes"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>首页轮播图列表</span>
      <el-button
        style="float: right;margin-right: 15px;clear: right"
        @click="$router.push('/ims/addSwiper')"
        size="small">
        添加
      </el-button>
    </el-card>
    <div class="table-container">
      <el-table ref="list"
                :data="list"
                style="width: 100%"
                v-loading="listLoading"
                border>
        <el-table-column label="编号" width="120" align="center">
          <template style="white-space: nowrap;" slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="标题" width="180" align="center">
          <template slot-scope="scope">
            {{ scope.row.title }}
          </template>
        </el-table-column>
        <el-table-column label="图片" align="center">
          <template slot-scope="scope">
            <img :src="scope.row.imgUrl" height="80" alt="">
          </template>
        </el-table-column>
        <el-table-column label="使用情况" style="text-overflow: ellipsis" width="180" align="center">
          <template style="white-space: nowrap;" slot-scope="scope">
            <el-select @change="changeState(scope.row)" v-model="scope.row.state" placeholder="请选择">
              <el-option
                v-for="item in swiperStateOption"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="类型" style="text-overflow: ellipsis" width="180" align="center">
          <template style="white-space: nowrap;" slot-scope="scope">{{ scope.row.type === 0 ? '商品' : '文章' }}</template>
        </el-table-column>
        <el-table-column label="操作" style="text-overflow: ellipsis" width="200" align="center">
          <template style="white-space: nowrap;" slot-scope="scope">
            <el-button icon="el-icon-edit" size="small" @click="updateRow(scope.row.id)" type="primary">修改</el-button>
            <el-button type="danger" icon="el-icon-delete" @click="deleteRow(scope.row.id)" size="small">删除</el-button>
          </template>
        </el-table-column>

      </el-table>

    </div>

    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5,10,15,20]"
        :current-page.sync="listQuery.pageNum"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {fetchList, swiperDelete, updateSwiper, updateSwiperState} from "@/api/swiper";

const defaultListQuery = {
  pageNum: 1,
  pageSize: 10,
  state: null,
  type: null,
  keyword: null
};

export default {
  name: "swiper",
  created() {
    this.getList()
  },
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: null,
      listLoading: false,
      swiperTypes: [
        {
          value: null,
          label: '全部'
        }, {
          value: 0,
          label: '商品'
        }, {
          value: 1,
          label: '文章'
        }],
      swiperStats: [
        {
          value: null,
          label: '全部'
        }, {
          value: 1,
          label: '轮播图1'
        }, {
          value: 2,
          label: '轮播图2'
        }, {
          value: 0,
          label: '未使用'
        }],
      swiperStateOption: [
        {
          value: 0,
          label: '未使用'
        },
        {
          value: 1,
          label: '轮播图1'
        }, {
          value: 2,
          label: '轮播图2'
        }
      ]
    }
  },
  methods: {
    getList() {
      this.listLoading = true;
      fetchList(this.listQuery).then(response => {
        this.listLoading = false;
        this.list = response.data.list;
        this.total = response.data.total;
      });
    },
    updateRow(id) {
      this.$router.push({
        path: '/ims/editSwiper',
        query: {id}
      })
    },
    deleteRow(id) {
      swiperDelete(id).then(res => {
        this.$message.success(res.message)
        this.getList()
      })
    },
    changeState(row) {
      updateSwiperState({
        id: row.id,
        state: row.state
      }).then(res => {
        this.$message.success(res.message)
      }).catch(res => {
        if (res.status === 303) {
          // console.log(res.data)
          row.state = res.data.state
        }
      })
    },
    handleSearchList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery);
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
  }
}
</script>

<style scoped>

</style>
